﻿using com.nm.Helper;
using com.nm.interfaces;
using com.nm.library.Factorys;
using System;
using System.Collections.Generic;
using System.Web;

namespace com.nm.library.com.nm.Helper
{
    public static class AppConfigHelper
    {
        private static IConfigurationFactory Facotry => FactoryMaker.Get<IConfigurationFactory>("ding-ConfigurationFactory",
            "com.nm.library.Factorys.RedisConfigurationFactory,com.nm.library");

        private static void SetNameSpace()
        {
            Facotry.NameSpace = ConfigurationHelper.Get("isDebug", false)
                ? "t:"
                : "d:";
        }

        public static IEnumerable<string> GetAllKeys()
        {
            SetNameSpace();
            return Facotry.AllKey;
        }

        public static IHttpHandler SetConfig(this IHttpHandler httpHandler, string pKey, string pValue)
        {
            SetNameSpace();
            Facotry.SetConfig(pKey, pValue);
            return httpHandler;
        }

        /// <summary>
        /// 从配置信息中获取非基础类型的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static T Get<T>(string pKey, T dv) where T : class, new()
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        /// <summary>
        /// 从配置信息中获取 字符串
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static string Get(string pKey, string dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        /// <summary>
        /// 从配置信息中获取 整形数值
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static int Get(string pKey, int dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        /// <summary>
        /// 从配置信息中获取 Boolean
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static bool Get(string pKey, bool dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        #region 扩展版本

        public static string GetConfig(this IHttpHandler httpHandler, string pKey)
        {
            SetNameSpace();
            return Facotry.GetValue(pKey);
        }

        public static T GetConfig<T>(this IHttpHandler httpHandler, string pKey, T dv) where T : class, new()
        {
            return Get(pKey, dv);
        }

        public static bool GetConfig(this IHttpHandler httpHandler, string pKey, bool dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        public static int GetConfig(this IHttpHandler httpHandler, string pKey, int dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        public static decimal GetConfig(this IHttpHandler httpHandler, string pKey, decimal dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        public static double GetConfig(this IHttpHandler httpHandler, string pKey, double dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        public static string GetConfig(this IHttpHandler httpHandler, string pKey, string dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        public static DateTime GetConfig(this IHttpHandler httpHandler, string pKey, DateTime dv)
        {
            SetNameSpace();
            return Facotry.Get(pKey, dv);
        }

        #endregion 扩展版本
    }
}